package com.augn.service.impl;

import com.augn.entity.PostEvaluateFrom;
import com.augn.entity.Result;
import com.augn.mapper.PostEvaluateFromMapper;
import com.augn.service.IPostEvaluateFromService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author augn
 * @since 2022-09-22
 */
@Service
public class PostEvaluateFromServiceImpl extends ServiceImpl<PostEvaluateFromMapper, PostEvaluateFrom> implements IPostEvaluateFromService {

    @Autowired
    private PostEvaluateFromMapper mapper;

    // 新增一个帖子评论的从评论
    @Override
    public Result<?> insertOnePostEvaluateFrom(PostEvaluateFrom postEvaluateFrom) {
        int insert = mapper.insert(postEvaluateFrom);
        if(insert==0){
            return Result.error("-1","新增失败");
        } else{
            return Result.success(insert);
        }
    }

    // 删除一个帖子评论的从评论
    @Override
    public Result<?> deleteOnePostEvaluateFrom(Integer id) {
        int delete = mapper.deleteById(id);
        if(delete==0){
            return Result.error("-1","删除失败");
        } else{
            return Result.success(delete);
        }
    }

    // 根据帖子主评论的id获取其从评论列表
    @Override
    public Result<?> getPartPostEvaluateFrom(Integer id) {
        QueryWrapper<PostEvaluateFrom> wrapper= new QueryWrapper<>();
        wrapper.eq("post_eva_main_id",id);
        List<PostEvaluateFrom> postEvaluateFromList = mapper.selectList(wrapper);
        if(postEvaluateFromList.isEmpty()){
            return Result.error("-1","暂无评论");
        } else{
            return Result.success(postEvaluateFromList);
        }
    }
}
